<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item> </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['goods:new:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:new:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:new:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['goods:new:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="newList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品图片" align="center" prop="goodsCoverImg">
        <template #default="scope">
          <ImagePreview
            :src="scope.row.goodsCoverImg"
            :height="40"
            :width="40"
          ></ImagePreview>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sortNum" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:new:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:new:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新品上线对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="goodsNewRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品" prop="goodsName">
          <div style="display: flex; align-items: center; width: 100%">
            <el-input
              v-model="form.goodsName"
              :disabled="true"
              style="margin-right: 10px"
            />
            <el-button type="primary" @click="selectGoodsClick"
              >选择商品</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sortNum">
          <el-input-number
            v-model="form.sortNum"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <SelectGoodsDialog
      v-model="selectGoodsVisiable"
      @select="selectGoodsCallback"
    ></SelectGoodsDialog>
  </div>
</template>

<script setup name="GoodsNew">
import {
  listNew,
  getNew,
  delNew,
  addNew,
  updateNew,
} from "@/api/goods/goodsNew.js";

import SelectGoodsDialog from "@/views/goods/goods/dialog/SelectGoodsDialog.vue";

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 商品轮播配置表格数据
const newList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

const data = reactive({
  form: {
    id: null,
    goodsId: null,
    goodsName: null,
    sortNum: null,
    remark: null,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    goodsId: null,
    sortNum: null,
    createId: null,
  },
  rules: {
    goodsName: [
      { required: true, message: "商品名称不能为空", trigger: "blur" },
    ],
    sortNum: [{ required: true, message: "排序不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品轮播配置列表 */
const getList = () => {
  loading.value = true;
  listNew(queryParams.value).then((response) => {
    newList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};
// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};
// 表单重置
const reset = () => {
  form.value = {
    id: null,
    goodsId: null,
    goodsName: null,
    sortNum: null,
    remark: null,
  };
  proxy.resetForm("goodsNewRef");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryForm");
  handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加新品上线配置";
};
/** 修改按钮操作 */
const handleUpdate = (row) => {
  reset();
  const id = row.id || ids.value;
  getNew(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改新品上线配置";
  });
};
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["goodsNewRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateNew(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNew(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除新品上线配置编号为"' + ids + '"的数据项？')
    .then(function () {
      return delNew(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};
/** 导出按钮操作 */
const handleExport = () => {
  proxy.download(
    "goods/goodsNew/export",
    {
      ...queryParams.value,
    },
    `goodsNew_${new Date().getTime()}.xlsx`
  );
};

const selectGoodsVisiable = ref(false);
const selectGoodsClick = () => {
  selectGoodsVisiable.value = true;
};

const selectGoodsCallback = (goods) => {
  form.value.goodsId = goods.id;
  form.value.goodsName = goods.goodsName;
};

getList();
</script>
